﻿Public Class FrmAMSMateriel

#Region " champs"
    Dim nom As String
    Dim pri, idT As Integer
    Dim macnx As New PPE3.ClassMySqlConnect("853339_depanneur", "071064")
    Dim nbchamps As Integer = 0
    Dim position As Integer
    Dim Num As String
#End Region

    Private Sub FrmAMSMateriel_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart
        Me.Left = 0
        Me.Top = 0

        macnx.Se_Connecter_MySqlServer()
        macnx.viderVue("prixMatos")
        DataGridViewliste.DataSource = RemplaceSlashApostrophesDataTable(macnx.Executer_Requete_Select("SELECT idMateriel as NumeroMateriel,NomMateriel, PrixMateriel FROM materiel", "prixMatos"))
        DataGridViewliste.AutoResizeColumns()

        BtnModif.BackColor = Color.Red
        macnx.Se_Deconnecter_MySqlServer()
    End Sub
    Public Sub Rafraichir()
        Try
            Dim donnees As New Data.DataTable
            macnx.viderVue("Matos")
            donnees = macnx.Executer_Requete_Select("Select NomMateriel, PrixMateriel from materiel where idMateriel = " & Num & "", "Matos")
            nbchamps = donnees.Rows.Count - 1
            TextBoxNomTrav.Text = RemplaceSlashApostrophes(macnx.GetChamp_DataTable(donnees, position, 0))
            TextBoxPrixTrav.Text = macnx.GetChamp_DataTable(donnees, position, 1)
            macnx.viderVue("Matos2")
            DataGridViewliste.DataSource = RemplaceSlashApostrophesDataTable(macnx.Executer_Requete_Select("SELECT idMateriel as NumeroMateriel,NomMateriel, PrixMateriel FROM materiel", "Matos2"))
            DataGridViewliste.ClearSelection()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    
    Private Sub RechargerLesDonnees()

        macnx.viderVue("prixMatos2")
        DataGridViewliste.DataSource = RemplaceSlashApostrophesDataTable(macnx.Executer_Requete_Select("SELECT idMateriel as NumeroMateriel,NomMateriel, PrixMateriel FROM materiel", "prixMatos2"))
    End Sub
#Region "Fonction"
    Public Function RemplaceSlashApostrophesDataTable(ByRef lesdonnees As DataTable)
        Dim nblignes As Integer
        Dim nbcolonnes As Integer
        Dim test As String = ""
        nblignes = lesdonnees.Rows.Count
        nbcolonnes = lesdonnees.Columns.Count
        Dim i, j As Integer
        For i = 0 To nblignes - 1
            For j = 0 To nbcolonnes - 1
                'Attention le replace ne s'applique qu'aux type String : utilisation de la classe de base Object :
                If Object.ReferenceEquals(lesdonnees.Rows(i).Item(j).GetType(), test.GetType()) Then
                    lesdonnees.Rows(i).Item(j) = RemplaceSlashApostrophes(lesdonnees.Rows(i).Item(j))
                End If
            Next
        Next
        Return lesdonnees
    End Function

    Public Function RemplaceApostrophesSlash(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("'", "/")
        Return ch
    End Function


    Public Function RemplaceSlashApostrophes(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("/", "'")
        Return ch
    End Function

#End Region

    Private Sub DataGridViewliste_Click(sender As Object, e As System.EventArgs) Handles DataGridViewliste.Click
        Try
            Dim id As Object = 0
            lblNumMat.Text = Me.DataGridViewliste.SelectedCells.Item(index:=id).Value.ToString
        Catch ex As Exception

        End Try
       
    End Sub


    Private Sub lblNumMat_TextChanged(sender As System.Object, e As System.EventArgs) Handles lblNumMat.TextChanged
        Num = lblNumMat.Text
        Try
            If lblNumMat.Text = "Materiel" Then
            Else

                macnx.Se_Connecter_MySqlServer()
                Rafraichir()
                macnx.Se_Deconnecter_MySqlServer()

            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtnAjout_Click(sender As System.Object, e As System.EventArgs) Handles BtnAjout.Click
        If BtnAjout.Text = "Ajout" Then
            macnx.Se_Connecter_MySqlServer()
            lblNumMat.Text = macnx.Generer_Cle_Primaire("idMateriel", "materiel")
            If lblNumMat.Text = "0" Then
                lblNumMat.Text = "1"
            End If
            macnx.Se_Deconnecter_MySqlServer()
            TextBoxNomTrav.Text = ""
            TextBoxPrixTrav.Text = ""
            BtnAjout.Text = "Valider!"
        Else
            If TextBoxNomTrav.Text <> "" Then
                idT = lblNumMat.Text
                nom = RemplaceApostrophesSlash(TextBoxNomTrav.Text)
                pri = TextBoxPrixTrav.Text
                macnx.Se_Connecter_MySqlServer()
                macnx.Executer_Requete_Action("Insert into materiel Values ( " & idT & ", '" & nom & "','" & pri & "')")
                MessageBox.Show("Ajout Réussi...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Information)
                BtnAjout.Text = "Ajout"
                TextBoxNomTrav.Text = ""
                TextBoxPrixTrav.Text = ""
                lblNumMat.Text = "Materiel"
                RechargerLesDonnees()
                macnx.Se_Deconnecter_MySqlServer()
            End If
        End If
    End Sub

    Private Sub BtnModif_Click(sender As System.Object, e As System.EventArgs) Handles BtnModif.Click
        If BtnModif.Text = "Modif" Then


            BtnModif.Text = "Valider!"
            BtnModif.BackColor = Color.Green
        Else
            If TextBoxNomTrav.Text <> "" Then

                idT = lblNumMat.Text
                nom = RemplaceApostrophesSlash(TextBoxNomTrav.Text)
                pri = TextBoxPrixTrav.Text
                macnx.Se_Connecter_MySqlServer()
                macnx.Executer_Requete_Action("update materiel Set NomMateriel = '" & nom & "', PrixMateriel ='" & pri & "' where IdMateriel =" & idT & "")
                MessageBox.Show("Modification Réussi...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Information)
                BtnModif.Text = "Modif"
                TextBoxNomTrav.Text = ""
                TextBoxPrixTrav.Text = ""
                lblNumMat.Text = "Materiel"
                BtnModif.BackColor = Color.Red
                RechargerLesDonnees()
                macnx.Se_Deconnecter_MySqlServer()
            End If
        End If
    End Sub

    Private Sub BtnSupp_Click(sender As System.Object, e As System.EventArgs) Handles BtnSupp.Click
        If TextBoxNomTrav.Text <> "" Then

            idT = lblNumMat.Text
            nom = RemplaceApostrophesSlash(TextBoxNomTrav.Text)
            pri = TextBoxPrixTrav.Text
            macnx.Se_Connecter_MySqlServer()
            macnx.Executer_Requete_Action("Delete from materiel Where idMateriel = " & idT & "")
            MessageBox.Show("Suppression Réussi...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TextBoxNomTrav.Text = ""
            TextBoxPrixTrav.Text = ""
            lblNumMat.Text = "Materiel"
            RechargerLesDonnees()
            macnx.Se_Deconnecter_MySqlServer()
        End If

    End Sub

    Private Sub BtnFermer_Click(sender As System.Object, e As System.EventArgs) Handles BtnFermer.Click
        Close()
    End Sub

  
    Private Sub BtnRetour_Click(sender As System.Object, e As System.EventArgs) Handles BtnRetour.Click
        Rafraichir()
    End Sub
End Class